package com.party.mem.biz.member;

import com.google.common.collect.Maps;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.core.model.commune.CIntegralType;
import com.party.core.model.commune.CUserIntegralMobileOutput;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.PartnerMember;
import com.party.core.service.commune.ICIntegralTypeService;
import com.party.core.service.commune.ICUserIntegralService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.mem.utils.RealmUtils;
import com.party.mem.web.dto.output.member.MemberMgmtOutput;
import com.party.mem.web.security.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员业务有关
 * @ClassName XzgsMemberBizService
 * @Description
 * @Author shenliuhai
 * @Date 2020/1/15 13:53
 **/
@Service
public class XzgsMemberBizService {


    @Autowired
    private ICIntegralTypeService integralTypeService;
    @Autowired
    private ICUserIntegralService userIntegralService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMemberGroupService memberGroupService;

    public static final String OUTDOOR_TYPE = "户外";
    public static final String STUDY_TYPE = "学习";
    public static final String DEVOTE_TYPE = "奉献";
    public static final String PULICE_WELFARE_TYPE = "公益";
    public static final String RECOMMEND_TYPE = "推荐";

    /**
     * 获取户外，公益，学习，奉献积分类型id
     * @author shenliuhai
     * @date 2020/1/15 14:39
     * @param parentId
     * @return CIntegralType
     */
    public CIntegralType getIntegralByType(String parentId, String type) {
        Map<String, Object> params = Maps.newHashMap();
        //户外
        params.put("createBy",parentId);
        params.put("name",type);
        List<CIntegralType> cIntegralTypes = integralTypeService.webPageList(params, null);
        return cIntegralTypes.get(0);

    }

    public MemberMgmtOutput getXzgsMemberInfo(PartnerMember partnerMember) {

        MemberGroup memberGroup = memberGroupService.get(partnerMember.getPartnerId());
        String parentId = memberGroupService.getPartnerId(partnerMember.getPartnerId());
        String mobile = memberService.get(partnerMember.getMemberId()).getMobile();
        String partnerId = partnerMember.getPartnerId();

        //获取当前登录用户
        CurrentUser currentUser = RealmUtils.getNewCurrentUser();

        MemberMgmtOutput output = new MemberMgmtOutput();

        //加入时间
        output.setCreateTime(DateUtils.formatDate(partnerMember.getCreateDate(),DateUtils.DATETIME_PATTERN));
        //所属分社
        output.setPartnerName(memberGroup.getRealname());

        Map<String, Object> params = new HashMap<>();

        List<MemberGroup> memberGroupList = new ArrayList<>();
        if (!currentUser.isChild()) {
            memberGroupList = memberGroupService.getChildWithMyAndName(partnerId);
            List<String> orgIds = LangUtils.transform(memberGroupList, input1 -> input1.getId());
            params.put("orgIds", orgIds);
            params.put("partnerType", PartnerMember.IS_MAIN_TYPE);
        } else if (currentUser.isXzgsAdmin()) {
            memberGroupList = memberGroupService.queryAdminBranchAndXZGSList(currentUser.getId(), currentUser.getPartnerId());
            List<String> orgIds = LangUtils.transform(memberGroupList, input1 -> input1.getId());
            params.put("orgIds", orgIds);
            params.put("partnerType", PartnerMember.IS_MAIN_TYPE);
        }  else {
            params.put("partnerId",partnerId);
        }

        params.put("cuiPartnerId", parentId);
        params.put("mobile",mobile);

        //影响力积分，分社内排名，总社排名
        output = getIntegralAndRanking(output,params,partnerId,Integer.valueOf(1));

        //行动力积分，分社内排名，总社排名
        output = getIntegralAndRanking(output,params,partnerId,Integer.valueOf(2));


        CIntegralType integralByType ;
        //户外，分社内排名，总社排名
        integralByType = getIntegralByType(parentId, OUTDOOR_TYPE);
        params.put("typeId",integralByType.getId());
        output = getIntegralAndRanking(output,params,partnerId,Integer.valueOf(3));

        //学习，分社内排名，总社排名
        integralByType = getIntegralByType(parentId, STUDY_TYPE);
        params.put("typeId",integralByType.getId());
        output = getIntegralAndRanking(output,params,partnerId,Integer.valueOf(4));

        //奉献，分社内排名，总社排名
        integralByType = getIntegralByType(parentId, DEVOTE_TYPE);
        params.put("typeId",integralByType.getId());
        output = getIntegralAndRanking(output,params,partnerId,Integer.valueOf(5));

        //公益，分社内排名，总社排名
        integralByType = getIntegralByType(parentId, PULICE_WELFARE_TYPE);
        params.put("typeId",integralByType.getId());
        output = getIntegralAndRanking(output,params,partnerId,Integer.valueOf(6));

        //推荐，分社内排名，总社排名
        integralByType = getIntegralByType(parentId, RECOMMEND_TYPE);
        params.put("typeId",integralByType.getId());
        output = getIntegralAndRanking(output,params,partnerId,Integer.valueOf(7));


        return output;
    }

    /**
     * @author shenliuhai
     * @date 2020/1/18 16:02
     * @param output
     * @param params
     * @param partnerId 分社id
     * @param type 1 影响力排名，2行动力排名 3 户外  4 学习  5 奉献  6 公益  7 推荐
     * @return com.party.mem.web.dto.output.member.MemberMgmtOutput
     */
    private MemberMgmtOutput getIntegralAndRanking(MemberMgmtOutput output,
                                                   Map<String, Object> params,String partnerId,Integer type) {
        //总积分
        Double totalScore = 0d;
        params.put("partnerType",PartnerMember.IS_MAIN_TYPE);
        params.put("partnerId",null);
        List<CUserIntegralMobileOutput> mobileOutputs = new ArrayList<>();
        if (Integer.valueOf(1).equals(type)) {
            //总社内排名
            mobileOutputs =
                    userIntegralService.queryIntegralInfluenceRankingListWithWeb(params, null);
            //总积分
            totalScore = mobileOutputs.get(0).getFraction();
            if (totalScore == null) {
                totalScore = 0d;
            }
            output.setMemberInfluenceForce(String.valueOf(totalScore));
            output.setMemberInfluenceForceRankTotal(mobileOutputs.get(0).getRanking().toString());

            //分社内排名
            params.put("partnerId",partnerId);
            params.put("partnerType",null);
            mobileOutputs =  userIntegralService.queryIntegralInfluenceRankingListWithWeb(params, null);
            output.setMemberInfluenceForceRank(mobileOutputs.get(0).getRanking().toString());

        } else if (Integer.valueOf(2).equals(type)) {
             mobileOutputs =
                    userIntegralService.queryUserIntegralWebList(params, null);
            totalScore = mobileOutputs.get(0).getFraction();
            if (totalScore == null) {
                totalScore = 0d;
            }
            output.setMemberActionForce(String.valueOf(totalScore));
            output.setMemberActionForceRankTotal(mobileOutputs.get(0).getRanking().toString());

            //分社内排名
            params.put("partnerId",partnerId);
            params.put("partnerType",null);
            mobileOutputs = userIntegralService.queryUserIntegralWebList(params, null);
            output.setMemberActionForceRank(mobileOutputs.get(0).getRanking().toString());
        } else if (Integer.valueOf(3).equals(type)) {
            //户外
            mobileOutputs =
                    userIntegralService.queryUserIntegralWebList(params, null);
            totalScore = mobileOutputs.get(0).getFraction();
            if (totalScore == null) {
                totalScore = 0d;
            }
            output.setOutdoorIntegral(String.valueOf(totalScore));
            output.setOutdoorIntegralRankTotal(mobileOutputs.get(0).getRanking().toString());

            params.put("partnerId",partnerId);
            params.put("partnerType",null);
            mobileOutputs = userIntegralService.queryUserIntegralWebList(params, null);
            output.setOutdoorIntegralRank(mobileOutputs.get(0).getRanking().toString());

        } else if (Integer.valueOf(4).equals(type)) {
            //学习
            mobileOutputs =
                    userIntegralService.queryUserIntegralWebList(params, null);
            totalScore = mobileOutputs.get(0).getFraction();
            if (totalScore == null) {
                totalScore = 0d;
            }
            output.setStudyIntegral(String.valueOf(totalScore));
            output.setStudyIntegralRankTotal(mobileOutputs.get(0).getRanking().toString());

            params.put("partnerId",partnerId);
            params.put("partnerType",null);
            mobileOutputs = userIntegralService.queryUserIntegralWebList(params, null);
            output.setStudyIntegralRank(mobileOutputs.get(0).getRanking().toString());

        } else if (Integer.valueOf(5).equals(type)) {
            //奉献
            mobileOutputs =
                    userIntegralService.queryUserIntegralWebList(params, null);
            totalScore = mobileOutputs.get(0).getFraction();
            if (totalScore == null) {
                totalScore = 0d;
            }
            output.setDevoteIntegral(String.valueOf(totalScore));
            output.setDevoteIntegralRankTotal(mobileOutputs.get(0).getRanking().toString());

            params.put("partnerId",partnerId);
            params.put("partnerType",null);
            mobileOutputs = userIntegralService.queryUserIntegralWebList(params, null);
            output.setDevoteIntegralRank(mobileOutputs.get(0).getRanking().toString());

        } else if (Integer.valueOf(6).equals(type)) {
            //公益
            mobileOutputs =
                    userIntegralService.queryUserIntegralWebList(params, null);
            totalScore = mobileOutputs.get(0).getFraction();
            if (totalScore == null) {
                totalScore = 0d;
            }
            output.setPublicWelfareIntegral(String.valueOf(totalScore));
            output.setPublicWelfareIntegralRankTotal(String.valueOf(mobileOutputs.get(0).getRanking()));

            params.put("partnerId",partnerId);
            params.put("partnerType",null);
            mobileOutputs = userIntegralService.queryUserIntegralWebList(params, null);
            output.setPublicWelfareIntegralRank(mobileOutputs.get(0).getRanking().toString());
        } else if (Integer.valueOf(7).equals(type)) {
            //推荐
            mobileOutputs =
                    userIntegralService.queryUserIntegralWebList(params, null);
            totalScore = mobileOutputs.get(0).getFraction();
            if (totalScore == null) {
                totalScore = 0d;
            }
            output.setRecommendIntegral(String.valueOf(totalScore));
            output.setRecommendIntegralRankTotal(mobileOutputs.get(0).getRanking().toString());

            params.put("partnerId",partnerId);
            params.put("partnerType",null);
            mobileOutputs = userIntegralService.queryUserIntegralWebList(params, null);
            output.setRecommendIntegralRank(mobileOutputs.get(0).getRanking().toString());
        }

        return output;
    }


}
